package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.RecordBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.GetScorePageContract;
import com.cgtong.android.mvp.model.GetScorePageModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by Tinwai on 2017/10/26.
 */
public class GetScorePagePresenter implements GetScorePageContract.Presenter {
    private GetScorePageContract.View mView;
    private GetScorePageContract.Model mModel;

    public GetScorePagePresenter(GetScorePageContract.View view) {
        this.mView = view;
        this.mModel = new GetScorePageModel();
    }

    @Override
    public void getScoreDatas(int maxId, int pageSize, final boolean isRefresh) {
        mModel.getScoreDatas(maxId, pageSize, new SimpleCallBack<List<RecordBean>>() {
            @Override
            public void onError(ApiException e) {
                if (isRefresh) {
                    mView.onNullData();
                } else {
                    mView.onError(e);
                }
            }

            @Override
            public void onSuccess(List<RecordBean> scoreBeanList) {
                mView.setRefreshing(false);
                if (isRefresh) {
                    if (scoreBeanList == null || scoreBeanList.size() == 0) {
                        mView.onNullData();
                        mView.setEnableLoadMore(false);
                        return;
                    } else if (scoreBeanList.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                        mView.setEnableLoadMore(false);
                    } else {
                        mView.setEnableLoadMore(true);
                    }
                    mView.setScoreDatas(isRefresh, scoreBeanList);
                } else {
                    mView.setScoreDatas(isRefresh, scoreBeanList);
                    if (scoreBeanList.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                        mView.setEnableLoadMore(false);
                        mView.onLoadMoreEnd(false);
                    } else {
                        mView.setEnableLoadMore(true);
                        mView.onLoadMoreEnd(true);
                    }
                }
            }
        });
    }
}
